package com.bestcem.xm.member.dao.impl.mysql;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.bestcem.xm.common.core.uitls.StringUtil;
import com.bestcem.xm.member.dao.FootprintDao;
import com.bestcem.xm.member.dao.mapper.FootprintMapper;
import com.bestcem.xm.member.entity.mysql.FootprintEntity;
import com.bestcem.xm.member.entity.pojo.FootprintDO;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ming.xue <ming.xue@idiaoyan.com>
 * @desc 足迹点数据访问层
 * @date 2021-08-17
 */
@Repository("footprintDao")
public class FootprintDaoImpl implements FootprintDao {

    @Resource
    private FootprintMapper footprintMapper;

    /**
     * 获取足迹点
     *
     * @param type
     * @param sourceKey
     * @return
     */
    @Override
    public FootprintDO get(Integer type, String sourceKey) {
        FootprintEntity entity = footprintMapper.findOneByTypeAndSourceKey(type, sourceKey);
        return FootprintDO.toDo(entity);
    }

    /**
     * 获取足迹点列表
     *
     * @param orgId
     * @param memberId
     * @return
     */
    @Override
    public List<FootprintDO> list(String orgId, String memberId) {
        List<FootprintEntity> entities = footprintMapper.findByOrgIdAndMemberId(orgId, memberId);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(entity -> FootprintDO.toDo(entity)).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public String saveOrUpdateFootprint(FootprintDO footprint) {
        if (StrUtil.isEmpty(footprint.getId())) {
            footprint.setId(StringUtil.getId());
            footprintMapper.saveFootprint(FootprintDO.toEntity(footprint));
        } else {
            footprintMapper.updateContentAndFootprintTimeById(FootprintDO.toEntity(footprint));
        }
        return footprint.getId();
    }

    @Override
    public List<FootprintDO> findByOrgId(String orgId) {
        List<FootprintEntity> entities = footprintMapper.findByOrgId(orgId);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(entity -> FootprintDO.toDo(entity)).collect(Collectors.toList());
        }
        return new ArrayList<FootprintDO>();
    }

    @Override
    public void batchSave(List<FootprintDO> newFootprintList) {
        if (CollUtil.isNotEmpty(newFootprintList)) {
            List<FootprintEntity> footprintEntities =
                    newFootprintList.stream().map(FootprintDO::toEntity).collect(Collectors.toList());
            footprintMapper.batchSave(footprintEntities);
        }
    }

    @Override
    public void batchSaveSampleData(List<FootprintDO> newFootprintList) {
        if (CollUtil.isNotEmpty(newFootprintList)) {
            List<FootprintEntity> footprintEntities =
                    newFootprintList.stream().map(FootprintDO::toEntity).collect(Collectors.toList());
            footprintMapper.batchSaveSampleData(footprintEntities);
        }
    }

    @Override
    public Long countFootprint(String orgId, String memberId) {
        return footprintMapper.countByOrgIdAndMemberId(orgId, memberId);
    }

    @Override
    public List<FootprintDO> list(String orgId, String memberId, Integer page, Integer size) {
        List<FootprintEntity> entities = footprintMapper.findPageByOrgIdAndMemberId(orgId, memberId, page, size);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(entity -> FootprintDO.toDo(entity)).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public List<FootprintDO> listFootprintsByIsCopy(int isCopyDemo, int sourceKey) {
        List<FootprintEntity> entities = footprintMapper.listFootprintsByIsCopy(isCopyDemo, sourceKey);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(entity -> FootprintDO.toDo(entity)).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }

    @Override
    public void updateContent(String id, String content) {
        footprintMapper.updateContent(id, content);
    }

    @Override
    public List<FootprintDO> findByOrgIdAndType(String orgId, int type) {
        List<FootprintEntity> entities = footprintMapper.findByOrgIdAndType(orgId, type);
        if (CollUtil.isNotEmpty(entities)) {
            return entities.stream().map(entity -> FootprintDO.toDo(entity)).collect(Collectors.toList());
        }
        return new ArrayList<>();
    }
}
